/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *    2010 year
 *    Author: Michael Khromov aka MikeDM (Mike D'Morto)
 *    E-Mail: mikedmorto@gmail.com
 */

#include "mrunner.h"
#include <QDebug>

Mrunner::Mrunner(QObject *parent) :
    QThread(parent)
{
    this->isFirstRun=true;

}

void Mrunner::startget(const QString & path, int ei){
    this->mpath=path;
    this->mei=ei;
    this->isGet=true;
    this->ret.clear();
    this->start();
}
void Mrunner::startput(const QString &path, int ei){
    this->mpath=path;
    this->mei=ei;
    this->isGet=false;
    this->start();
}

void Mrunner::run(){
        if(this->isFirstRun){
            this->proc = new QProcess();
            connect(this->proc,SIGNAL(readyReadStandardOutput()),SLOT(slotRead()));
            connect(this->proc,SIGNAL(finished(int)),SLOT(slotFin(int)));
            this->isFirstRun=false;
        }

        qDebug()<<"started "<<this->mpath;
        this->proc->start(this->mpath);
        if(!this->proc->waitForStarted(1000)){
            qDebug()<<"Error start";
            if(this->isGet){
                emit this->sigErrorGet();
            }else{
                emit this->sigErrorPut();
            }
        }else{
            schet=1;
            while(!this->proc->waitForFinished(1000)){
                qDebug()<<"Wait ... "<<schet;
                schet++;
                if(schet >= this->mei){
                    if(this->isGet){
                        emit this->sigErrorGet();
                    }else{
                        emit this->sigErrorPut();
                    }
                    this->proc->close();
                    break;
                }

            }
            qDebug()<<"Exit state"<<this->proc->exitStatus();
            if(this->proc->exitStatus() != 0 ){
                // ошибка работы
                if(this->isGet){
                    emit this->sigErrorGet();
                }else{
                    emit this->sigErrorPut();
                }
            }else{
                //отработал нормально. отдаем строку
                if(this->isGet){
                    //emit this->sigSuccessGet();
                }else{
                    //emit this->sigSuccessPut();
                }
            }

            //delete this->proc;
        }



}

void Mrunner::slotRead(){
    qDebug()<<"STD OUT BZZZ";
    schet=1;

}

void Mrunner::slotFin(int err){
    //qDebug()<<"FIN - ERR "<<err;
    //qDebug()<<this->proc->readAllStandardOutput();
    if(err==0){
        if(this->isGet){
            // вылавливаем строку статуса
            while(1){

                QString aa = this->proc->readLine();
                if(aa.isEmpty() ){break;}
                //qDebug()<<"STD OUT "<<aa;
                QRegExp re("^mutester\:");
                //qDebug()<<"Contains id = "<<aa.contains(re);
                if(aa.contains(re) == 1){
                    // содержит "mutester:" значит наш клиент.
                    this->ret = aa.mid(9,aa.size());
                    //qDebug()<<"INSERTED "<<aa;
                }

            }
            emit this->sigSuccessGet();
        }else{
            emit this->sigSuccessPut();
        }
    }
//    delete this->proc;



}
